﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CommonTagTime.PagerControl;
using CommonTagTime.Business;
using CommonTagTime.ModelObject;
using CommonTagTime;
using System.Resources;
using System.IO;

public partial class admin_BannerList : SitePage
{
    #region ViewState
    public bool isShowEffect = false;
    public int count = 0;
    public int F_PageIndex
    {
        get
        {
            object o = ViewState["F_PageIndex"];
            if (o != null) return (int)o;
            return 0;
        }
        set { ViewState["F_PageIndex"] = value; }
    }
    public int F_PageSize
    {
        get
        {
            object o = ViewState["F_PageSize"];
            if (o != null) return (int)o;
            return 0;
        }
        set { ViewState["F_PageSize"] = value; }
    }
    public string F_SortField
    {
        get
        {
            object o = ViewState["F_SortField"];
            if (o != null) return (string)o;
            return string.Empty;
        }
        set { ViewState["F_SortField"] = value; }

    }
    public string paraURL
    {
        get
        {
            object o = ViewState["paraURL"];
            if (o != null) return (string)o;
            return string.Empty;
        }
        set { ViewState["paraURL"] = value; }

    }
    public string F_SortExpression
    {
        get
        {
            object o = ViewState["F_SortExpression"];
            if (o != null) return (string)o;
            return string.Empty;
        }
        set { ViewState["F_SortExpression"] = value; }
    }
    #endregion
    #region Init,Loads
    protected void Page_Load(object sender, EventArgs e)
    {
        ltrError.Visible = false;
        if (!Page.IsPostBack)
        {
            clsBindControl.BindBooleanCombobox(F_ddlIsAvtive);
            GetParamater();
            LoadData();
        }
    }

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
    }
    #endregion

    #region Events
    protected void Pager2_PageIndexChanging(object obj, PageIndexChangeEventArgs e)
    {
        this.F_PageIndex = e.PageIndex;
        LoadData();
    }
    protected void Pager2_PageSizeChanging(object obj, PageSizeChangeEventArgs e)
    {
        this.F_PageIndex = 1;
        this.F_PageSize = Pager2.PageSize = Pager1.PageSize = e.PageSize;
        LoadData();
    }
    protected void rptData_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Banner objData = (Banner)e.Item.DataItem;
            if (objData == null)
                return;
            hidID.Value += objData.Id.ToString() + ";";

            //image button IsActive
            ImageButton imgButton = (ImageButton)e.Item.FindControl("imbActive");
            imgButton.CommandArgument = objData.Id.ToString();
            if (objData.IsActive == true)
            {
                imgButton.ImageUrl = clsString.ACTIVE_IMAGE_URL;
                imgButton.ToolTip = Resources.Resource.TIP_DEACTIVE;
                imgButton.CommandName = "DeActive";
            }
            else
            {
                imgButton.ImageUrl = clsString.DEACTIVE_IMAGE_URL;
                imgButton.ToolTip = Resources.Resource.TIP_ACTIVE;
                imgButton.CommandName = "Active";
            }
            //image button Order
            imgButton = (ImageButton)e.Item.FindControl("imbOrderDown");
            if (e.Item.ItemIndex + 1 == count)
                imgButton.Visible = false;
            else
            {
                imgButton.ToolTip = Resources.Resource.TIP_MOVEDOWN;
                imgButton.CommandArgument = objData.Id.ToString();
                imgButton.ImageUrl = clsString.ORDERDOWN_IMAGE_URL;
            }
            imgButton = (ImageButton)e.Item.FindControl("imbOrderUp");
            if (e.Item.ItemIndex == 0)
                imgButton.Visible = false;
            else
            {
                imgButton.CommandArgument = objData.Id.ToString();
                imgButton.ToolTip = Resources.Resource.TIP_MOVEUP;
                imgButton.ImageUrl = clsString.ORDERUP_IMAGE_URL; ;
                imgButton.Visible = true;
            }
            Literal ltrStartDate = (Literal)e.Item.FindControl("ltrStartDate");
            if (ltrStartDate != null)
            {
                ltrStartDate.Text = clsConvert.String(objData.StartingDate);
            }
            Literal ltrEndDate = (Literal)e.Item.FindControl("ltrEndDate");
            if (ltrEndDate != null)
            {
                ltrEndDate.Text = clsConvert.String(objData.EndingDate);
            }
            //Image
            Literal ltrImage = (Literal)e.Item.FindControl("ltrImage");
            if (ltrImage != null)
            {
                string pathImage = Server.MapPath("~/" + clsConfigData.DEFAULT_UPLOAD_BANNER_FOLDER) + objData.FilePath;
                if (File.Exists(pathImage))
                {
                    ltrImage.Text = "<img src='" + "../" + clsConfigData.DEFAULT_UPLOAD_BANNER_FOLDER + objData.FilePath + "' alt='" + objData.BannerName + "'  height='100px' />";
                }
            }
            imgButton = (ImageButton)e.Item.FindControl("imbDelete");
            imgButton.ToolTip = Resources.Resource.TIP_DELETE;
            imgButton.Attributes.Add("onclick", "return confirm('" + Resources.Resource.MSG_CONFIRM_DELETE + "');");
            imgButton.CommandArgument = objData.Id.ToString();
            imgButton.ImageUrl = clsString.DELETE_IMAGE_URL;
            System.Web.UI.HtmlControls.HtmlTableRow row = (System.Web.UI.HtmlControls.HtmlTableRow)e.Item.FindControl("trRow");
            if (e.Item.ItemIndex % 2 == 0)
                row.Attributes.Add("class", "alt-row");
        }

    }
    protected void rptData_ItemCommand(object source, RepeaterCommandEventArgs e)
    {

        if (e.CommandName == "SortOrder")
        {
            SortList(e.CommandArgument.ToString());

        }
        else
        {
            int id = clsConvert.Int(e.CommandArgument.ToString());
            switch (e.CommandName)
            {

                case "OrderUp":
                    {
                        OrderUp(id);
                        break;
                    }
                case "OrderDown":
                    {
                        OrderDown(id);
                        break;
                    }
                case "Active":
                    {
                        Active(id);
                        break;
                    }
                case "DeActive":
                    {
                        DeActive(id);
                        break;
                    }

                case "Delete":
                    {
                        if (Delete(id))
                            LoadData();
                        break;
                    }
            }
        }
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        this.F_SortField = busBanner.SortOrder;
        this.F_SortExpression = "ASC";
        this.F_PageIndex = 1;
        LoadData();
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        string[] arr = hidIDSelect.Value.Split(';');
        for (int i = 0; i < arr.Length; i++)
        {
            if (arr[i].ToString() != string.Empty)
            {
                Delete(clsConvert.Int(arr[i].ToString()));
            }
        }
        LoadData();
    }
    protected void btnAddNew_Click(object sender, EventArgs e)
    {
        Response.Redirect("BannerDetail.aspx?" + paraURL);
    }
    #endregion

    #region Methods
    private void GetParamater()
    {
        F_txtName.Text = clsQueryString.GetString("F_txtName");
        F_txtStartDate.Text = clsQueryString.GetString("F_txtStartDate");
        F_txtEndDate.Text = clsQueryString.GetString("F_txtEndDate");
        this.F_SortField = clsQueryString.GetString("F_SortField");
        if (this.F_SortField == string.Empty)
            this.F_SortField = busBanner.SortOrder;
        this.F_SortExpression = clsQueryString.GetString("F_SortExpression");
        if (this.F_SortExpression == string.Empty)
            this.F_SortExpression = "ASC";
        F_PageIndex = clsQueryString.GetInt("F_PageIndex");
        F_PageSize = clsQueryString.GetInt("F_PageSize");
        if (F_PageSize < 1)
            F_PageSize = clsConfigData.DEFAULT_ADMIN_PAGE_SIZE;
        if (F_PageIndex < 1)
            F_PageIndex = 1;
        this.Pager2.PageSize = this.Pager1.PageSize = F_PageSize;
        F_ddlIsAvtive.SelectedValue = clsQueryString.GetString("F_ddlIsAvtive");
    }
    private void SortList(string F_SortField)
    {
        if (this.F_SortField == F_SortField)
        {
            if (this.F_SortExpression == "ASC")
                this.F_SortExpression = "DESC";
            else this.F_SortExpression = "ASC";
        }
        else
        {
            this.F_SortField = F_SortField;
            this.F_SortExpression = "ASC";
        }
        LoadData();
    }
    private void OrderDown(int id)
    {
        busBanner objBus = new busBanner();
        if (objBus.OrderDown(id))
            LoadData();
    }
    private void OrderUp(int id)
    {

        busBanner objBus = new busBanner();
        if (objBus.OrderUp(id))
            LoadData();
    }
    private void Active(int id)
    {
        busBanner objBus = new busBanner();
        if (objBus.Active(id))
            LoadData();
    }
    private void DeActive(int id)
    {
        busBanner objBus = new busBanner();
        if (objBus.DeActive(id))
            LoadData();
    }
    private bool Delete(int id)
    {
        //get Image
        busBanner objBus = new busBanner();
        Banner objTip = objBus.GetByID(id);
        if (objTip != null)
        {
            //Delete Data
            if (objBus.Delete(id))
            {
                //Delete Image
                DeleteImage(objTip.FilePath);
                return true;
            }
        }
        return false;
    }
    private void DeleteImage(string _orgImageFileName)
    {
        string path = Server.MapPath("~/" + clsConfigData.DEFAULT_UPLOAD_BANNER_FOLDER);
        clsFile.DeleteFile(path + _orgImageFileName);
        //path = Server.MapPath("~/" + clsConfigData.DEFAULT_UPLOAD_BANNER_THUMB_FOLDER);
        //clsFile.DeleteFile(path + _orgImageFileName); 
    }
    private void LoadData()
    {

        paraURL = GetPageParams(FilterFieldType.All);
        hidID.Value = string.Empty;
        //get Condition
        Banner objLogo = new Banner();
        objLogo.CurrentPage = this.F_PageIndex;
        objLogo.OrderBy = this.F_SortField;
        objLogo.OrderDirection = this.F_SortExpression;
        objLogo.PageSize = Pager1.PageSize;
        objLogo.Condition = " 1=1 ";
        if (F_txtName.Text != string.Empty)
            objLogo.Condition += " and b.BannerName like N'%" + F_txtName.Text + "%'";
        if (F_ddlIsAvtive.SelectedValue != string.Empty)
            objLogo.Condition += " and b.IsActive=" + F_ddlIsAvtive.SelectedValue.ToString();
        DateTime dtStart = clsConvert.Date(F_txtStartDate.Text);
        DateTime dtEnd = clsConvert.Date(F_txtEndDate.Text);
        if (dtStart != DateTime.MinValue)
        {
            objLogo.Condition += " and datediff(day,'" + clsConvert.String(dtStart) + "',b.StartingDate)>=0";
        }
        else
            F_txtStartDate.Text = string.Empty;

        if (dtEnd != DateTime.MinValue)
        {
            objLogo.Condition += " and datediff(day,b.EndingDate,'" + clsConvert.String(dtEnd) + "')>=0";
        }
        else
            F_txtEndDate.Text = string.Empty;
        busBanner _busBanner = new busBanner();
        int total = 0;
        BannerCollection lstResult = _busBanner.List(objLogo);
        total = objLogo.TotalRecords;
        if (lstResult != null && lstResult.Count > 0)
        {
            count = lstResult.Count;
            rptData.DataSource = lstResult;
            divGrid.Visible = true;
            divEmplty.Visible = false;
        }
        else
        {
            rptData.DataSource = null;
            divGrid.Visible = false;
            divEmplty.Visible = true;
            divEmplty.InnerHtml = Resources.Resource.MSG_EMPTY;
        }

        rptData.DataSource = lstResult;
        rptData.DataBind();
        Pager1.SetPaging(this.F_PageIndex, count, total);
        Pager2.SetPaging(this.F_PageIndex, count, total);

        if (lstResult.Count > 0)
        {
            isShowEffect = true;
            LoadEffect();
        }
    }
    private void LoadEffect()
    {
        //Load List Effect
        busSlideEffect _busSlideEffect = new busSlideEffect();
        SlideEffectCollection effect = _busSlideEffect.GetAll();
        cblListEffect.DataSource = effect;
        cblListEffect.DataValueField = "Code";
        cblListEffect.DataTextField = "Name";
        cblListEffect.DataBind();
        LoadSelectedEffect();
    }
    private void LoadSelectedEffect()
    {
        busBannerSlideConfig _busBannerSlideConfig = new busBannerSlideConfig();
        BannerSlideConfigCollection lstEffect = _busBannerSlideConfig.GetAll();
        foreach (var item in lstEffect)
        {
            foreach (ListItem i in cblListEffect.Items)
            {
                if (item.SlideEffect.Equals(i.Value))
                {
                    i.Selected = true;
                     break;
                }
            }
        }
    }
    protected void btSaveEffect_Click(object sender, EventArgs e)
    {
        isShowEffect = true;
        int result = 0;
        busBannerSlideConfig _busBannerSlideConfig = new busBannerSlideConfig();
        ListItemCollection lstItem = cblListEffect.Items;
        foreach (ListItem item in lstItem)
        {
            if (item.Selected)
            {
                result++;
                break;
            }
        }
        if (result > 0)
        {
            _busBannerSlideConfig.DeleteAll();
            foreach (ListItem item in lstItem)
            {
                if (item.Selected)
                {
                    _busBannerSlideConfig.Insert(item.Value);
                }
            }
            LoadSelectedEffect();
            //lblErrorMessage.Text = "";
            ltrError.Visible = false;
        }
        else
        {
            //lblErrorMessage.Text = "No item selected.";
            ltrError.Text = "You must select effect.";
            ltrError.Visible = true;
        }

    }

    #endregion

}